package edu.chinasoft.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.annotation.WebServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import edu.chinasoft.domain.Application;
import edu.chinasoft.domain.PageUnitForApplication;
import edu.chinasoft.domain.User;
import edu.chinasoft.service.ApplicationService;

/**
 *
 * @author mhao
 * @date 2020年2月12日:下午3:56:48
 * @version V1.0
 * @Description: 
*/
@WebServlet(name = "myShowApplicationServlet", value = "/servlet/showapplicationservlet")
public class ShowApplicationServlet extends HttpServlet {
	private static Logger logger = LoggerFactory.getLogger(ShowApplicationServlet.class);
	private ApplicationService applicationService;
	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info(" ShowApplicationServlet >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		//获取分页单位
		ServletContext servletContext = this.getServletContext();
		String initParameter = servletContext.getInitParameter("pageUnit");
		int pageUnit = Integer.parseInt(initParameter);
		//获取当前页
		String pageParameter = request.getParameter("currentPage");
		pageParameter=pageParameter==null?"1":pageParameter;
		int currentPage = Integer.parseInt(pageParameter);
		//获取当前登录用户
		HttpSession session = request.getSession(false);
		User userLogin = (User)session.getAttribute("userLogin");
		logger.info("当前登录用户为："+userLogin);
		Map<String, Object> map = new HashMap<>();
		int setLimit = (currentPage-1)*pageUnit;
		map.put("setLimit",setLimit);
		map.put("pageUnit",pageUnit);
		map.put("userId",userLogin.getUserId());
		logger.info("userId："+map.get("userId"));
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		ApplicationService applicationServiceImp = wac.getBean("applicationService", ApplicationService.class);
		this.applicationService = applicationServiceImp;
		//调用服务层方法
		PageUnitForApplication pageUnitForApplication = applicationService.getRecordNumById(userLogin);
		logger.info("userId："+pageUnitForApplication.getRecordNum());
		List<Application> applicationList = applicationService.selectUnitApplicationById(map);
		logger.info("applicationList："+applicationList);
		//修改日期格式
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Map<Integer, Map> allTimeMap = new HashMap<Integer, Map>();
		//所有申请表的时间字典
		for (Application application:applicationList){
			Map<String, String> applicationTimeMap = new HashMap<String, String>();
			//每个申请表的时间字典
			Date applicationBeginDate = application.getApplicationBegin();
			Date applicationEndDate = application.getApplicationEnd();
			String applicationBeginString = simpleDateFormat.format(applicationBeginDate);
			String applicationEndString = simpleDateFormat.format(applicationEndDate);
			logger.info(applicationBeginString+"："+applicationEndString);
			applicationTimeMap.put("applicationBeginString",applicationBeginString);
			applicationTimeMap.put("applicationEndString",applicationEndString);
			logger.info(applicationTimeMap.get("applicationBeginString")+":"+applicationTimeMap.get("applicationEndString"));
			allTimeMap.put(application.getApplicationId(),applicationTimeMap);
			logger.info("记录:"+allTimeMap.get(application.getApplicationId()));
		}
		 logger.info("字典大小为："+allTimeMap.size());
			Set<Integer> keySet = allTimeMap.keySet();
			for (Integer key:keySet){
				logger.info(key+"："+allTimeMap.get(key));
			}
			session.setAttribute("allTimeMap",allTimeMap);
			pageUnitForApplication.setApplicationList(applicationList);
			pageUnitForApplication.setPageUnit(pageUnit);
			pageUnitForApplication.setCurrentPage(currentPage);
			//加到session域里
			session.setAttribute("applicationPage", pageUnitForApplication);
			response.sendRedirect(""+request.getContextPath()+"/showApplication.jsp");
		logger.info(" ShowApplicationServlet >doGet end.......");
	}

	/**
	 *
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info(" ShowApplicationServlet >doPost start.......");
		this.doGet(request, response);
		logger.info(" ShowApplicationServlet >doPost end.......");
	}

}